#include "stdafx.h"
#include "Waving Hands.h"
#include "Setting.h"
#include "TablesData.h"


Setting::Setting(CWnd* pParent /*=NULL*/)
	: CDialog(Setting::IDD, pParent)
{
	//{{AFX_DATA_INIT(Setting)
	m_PlayerName = _T("");
	m_ServerFlag = FALSE;
	m_ClientFlag = FALSE;
	m_NetworkFlag = FALSE;
	m_ServerNameValue = _T("");
	m_ServerPortValue = _T("");
	//}}AFX_DATA_INIT
}


void Setting::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(Setting)
	DDX_Control(pDX, IDC_Test, m_Test);
	DDX_Control(pDX, IDC_ServerPort, m_ServerPort);
	DDX_Control(pDX, IDC_ServerNameStatic, m_ServerNameStatic);
	DDX_Control(pDX, IDC_ServerName, m_ServerName);
	DDX_Control(pDX, IDC_Server, m_Server);
	DDX_Control(pDX, IDC_PortStatic, m_PortStatic);
	DDX_Control(pDX, IDC_PlayerNameStatic, m_PlayerNameStatic);
	DDX_Control(pDX, IDC_Network, m_Network);
	DDX_Control(pDX, IDC_LanguageStatic, m_LanguageStatic);
	DDX_Control(pDX, IDC_Language, m_Language);
	DDX_Control(pDX, IDC_Connection, m_Connection);
	DDX_Control(pDX, IDC_Client, m_Client);
	DDX_Text(pDX, ID_PlayerName, m_PlayerName);
	DDX_Check(pDX, IDC_Server, m_ServerFlag);
	DDX_Check(pDX, IDC_Client, m_ClientFlag);
	DDX_Check(pDX, IDC_Network, m_NetworkFlag);
	DDX_Text(pDX, IDC_ServerName, m_ServerNameValue);
	DDX_Text(pDX, IDC_ServerPort, m_ServerPortValue);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(Setting, CDialog)
	//{{AFX_MSG_MAP(Setting)
	ON_BN_CLICKED(IDC_Test, OnTest)
	ON_BN_CLICKED(IDC_Network, OnNetwork)
	ON_BN_CLICKED(IDC_Server, OnServer)
	ON_BN_CLICKED(IDC_Client, OnClient)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Setting message handlers
BOOL Setting::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	if(m_NetworkFlag==false)
	{
	 m_ServerName.EnableWindow(false);
	 m_ServerPort.EnableWindow(false);
	 m_Server.EnableWindow(false);
	 m_Client.EnableWindow(false);
	 m_ServerFlag=false;
	 m_ClientFlag=false;
	 m_NetworkFlag=false;
	 UpdateData(false);

	} 

	m_Language.ResetContent();
	for(int i=0;i<Language;i++)
		m_Language.AddString(LanguageName[i][lang]);

	m_Language.SetCurSel(lang);

	m_PlayerNameStatic.SetWindowText(ButtonName[Button_Name][lang]);
	m_LanguageStatic.SetWindowText(ButtonName[Button_Language][lang]);
	m_ServerNameStatic.SetWindowText(ButtonName[Button_Server][lang]);
	m_PortStatic.SetWindowText(ButtonName[Button_Port][lang]);
	m_Client.SetWindowText(ButtonName[Button_Client][lang]);
	m_Server.SetWindowText(ButtonName[Button_Server][lang]);
	m_Test.SetWindowText(ButtonName[Button_Test][lang]);
	m_Network.SetWindowText(ButtonName[Button_Network][lang]);
	m_Connection.SetWindowText(ButtonName[Button_Conection][lang]);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void Setting::OnNetwork() 
{

	if(m_NetworkFlag==false)
	{
 	 m_ServerName.EnableWindow(true);
	 m_ServerPort.EnableWindow(true);
	 m_Server.EnableWindow(true);
	 m_Client.EnableWindow(true);
	 m_ServerNameValue="loopback";
	 m_ServerPortValue="4000";
	 m_ServerFlag=true;
	 m_NetworkFlag=true;
	 UpdateData(false);
	}
	else
	{
	 m_ServerName.EnableWindow(false);
	 m_ServerPort.EnableWindow(false);
	 m_Server.EnableWindow(false);
	 m_Client.EnableWindow(false);
	 m_ServerFlag=false;
	 m_ClientFlag=false;
	 m_NetworkFlag=false;
	 UpdateData(false);
	}

}

void Setting::OnClient() 
{
	m_Test.EnableWindow(true);
	m_ServerFlag=false;
	m_ClientFlag=true;
	UpdateData(false);

}

void Setting::OnServer() 
{
	m_Test.EnableWindow(false);
    m_ServerFlag=true;
	m_ClientFlag=false;
	UpdateData(false);

}

void Setting::OnTest() 
{
	char chBuf[26];
	CSocket sMy;
	sMy.Create();
//	sMy.Connect("mail.tut.by", 25);
	sMy.Connect(m_ServerNameValue,atoi(m_ServerPortValue));
	sMy.Receive(chBuf, 25);
	AfxMessageBox(chBuf);
	sMy.Close();
}

void Setting::OnOK() 
{
	CString Name;

	lang=m_Language.GetCurSel();

	GetDlgItem(ID_PlayerName)->GetWindowText(Name);
	if(Name!="")
	{
	 if(Name.GetLength()<=10)
		CDialog::OnOK();
	 else
		AfxMessageBox(SystemMessage[Sys_longname][lang]);
	}
	else
		AfxMessageBox(SystemMessage[Sys_Inadname][lang]);
	
}

void Setting::OnCancel() 
{
	OnOK();
}
